#$dir ="/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/programtest";
#$rfile = "$dir/whole_r2";
#$bfile = "/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle_x_chr/beagle_x.tped";
#$afile = "/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle_x_chr/beagle_x_ambi.tped";
#$hfile = "/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle_x_chr/beagle_x_nohapmap.tped";
#$hafile = "$dir/whole_no_snp_hapmap.txt";
#$file_tped = "$dir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped";
#$file_gprobs = "$dir/whole_chr_gprobs_dosage2";

$dir = $ARGV[0];
$rfile = $ARGV[1];
$bfile = $ARGV[2];
#$afile = $ARGV[3];
#$hfile = $ARGV[4];
#$hafile= $ARGV[5];
$file_tped = $ARGV[3];
$file_gprobs = $ARGV[4];

open BUFF, "< $file_gprobs" or die "Cant open $file_gprobs";
open TBUFF,"< $file_tped" or die "Cant open $file_tped";
open RBUFF, "< $rfile" or die "Cant open $rfile";
open BBUFF, "< $bfile" or die "Cant open $bfile";
#open ABUFF, "< $afile" or die "Cant open $afile";
#open HBUFF, "< $hfile" or die "Cant open $hfile";
#open HABUFF, "< $hfile" or die "Cant open $hafile";
open(WRBUFF,">$dir/whole_indy");
#$line = <BUFF>;
#@ar = split(/\s/,$line);
#shift(@ar);
#shift(@ar);
#shift(@ar);
#$line = join(" ",@ar);
#$line = "SNPID QUALITYSCORE $line\n";
#print WRBUFF $line;
while($rline = <RBUFF>)
{
	chomp($rline);
	@rarray = split(/\t/,$rline);
	if($rarray[1] ne "NaN")
	{
		#print $hash_r{$rarray[0]}."\t".$rarray[0]."\n";
		$hash_r{$rarray[0]} = $rarray[1];
	}
}
while($line = <BBUFF>)
{
	chomp($line);
	@array = split(/\s/,$line);
	$hash_snp{$array[1]} = 1;
	#print "n $line\n";
	shift(@array);
	$rssnp =  shift(@array);
	shift(@array);
	shift(@array);
	$llline = join(" ",@array);
	if($llline =~ m/0\s0/)
	{
		$hash_snp{$rssnp} = 0;
		$hash{$rssnp} = $line;
	#	print $hash{$rssnp}."\t".$hash_snp{$rssnp}."\n";
	}
}
=head
while($line = <ABUFF>)
{
        chomp($line);
        @array = split(/\s/,$line);
        $hash_snp{$array[1]} = 1;
	shift(@array);
        $rssnp =  shift(@array);
        shift(@array);
        shift(@array);
        $llline = join(" ",@array);
        if($llline =~ m/0\s0/)
        {
                $hash_snp{$rssnp} = 0;
                $hash{$rssnp} = $line;
        }
}
while($line = <HBUFF>)
{
        chomp($line);
        @array = split(/\s/,$line);
        $hash_snp{$array[1]} = 1;
}
while($line = <HABUFF>)
{
        chomp($line);
        @array = split(/\s/,$line);
        $hash_snp{$array[1]} = 1;
}
=cut
while($tline = <BUFF>)
{
	chomp($tline);
	@tarray = split(/\s/,$tline);
	#shift(@tarray);
	$snp =  shift(@tarray);
	 shift(@tarray);
	 shift(@tarray);
	$line = $snp." ".$hash_r{$snp};
	#print " $snp\t$hash_snp{$snp}\n"; 
	if($hash_snp{$snp} eq "1")
	{
	#	print "success1\n";
		for($i=0;$i<@tarray;$i++)
		{
			$line = $line." "."0";
		}	
	}
	elsif($hash_snp{$snp} eq "0")
	{
		print "$snp success2\n";
		$lline = $hash{$snp};
		@arr = split(/\s/,$lline);
		shift(@arr);
		shift(@arr);
		shift(@arr);
		shift(@arr);
		for($h=0;$h<@arr;$h++)
		{
			if($arr[$h] eq "0" )
			{
				$line = $line." "."1";
			}
			else
			{
				$line = $line." "."0";
			}
			$h++;
		}
	}
	else
	{
	#	print "success3\n";
		for($i=0;$i<@tarray;$i++)
                {
                        $line = $line." "."1";
                }

	}
	print WRBUFF $line."\n";
	
}
